package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.PageResults;
import com.itheima.common.R;
import com.itheima.dto.PePermissionGroup;
import com.itheima.dto.Permissionss;
import com.itheima.mapper.PermissionGroupMapper;
import com.itheima.mapper.PermissionPermissionGroupMapper;
import com.itheima.pojo.PermissionGroup;
import com.itheima.pojo.PermissionPermissionGroup;
import com.itheima.service.PermissionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class PermissionsServiceImpl implements PermissionsService {
    @Autowired
    private PermissionPermissionGroupMapper permissionPermissionGroupMapper;

    @Autowired
    private PermissionGroupMapper permissionGroupMapper;
    //权限组列表
    @Override
    public PageResults findBypage(Integer page, Integer pageSize , String keyword) {

        IPage<PermissionGroup> p = new Page<>(page, pageSize);
        if (keyword != null) {
            LambdaQueryWrapper<PermissionGroup> wrapper = new LambdaQueryWrapper<>();
            wrapper.like(PermissionGroup::getName, keyword);
        }
        LambdaQueryWrapper<PermissionGroup> wrapper = new LambdaQueryWrapper<>();
        IPage<PermissionGroup> page1 = permissionGroupMapper.selectPage(p, wrapper);
        List<PermissionGroup> records = page1.getRecords();
        String s = JSON.toJSONString(records);
        List<PePermissionGroup> pePermissionGroups = JSON.parseArray(s, PePermissionGroup.class);
        for (PePermissionGroup pePermissionGroup : pePermissionGroups) {
            pePermissionGroup.setTitle(pePermissionGroup.getName());
            pePermissionGroup.setCreate_date(pePermissionGroup.getCreateTime());
            pePermissionGroup.setUpdate_date(pePermissionGroup.getUpdateTime());
        }
        return new PageResults((int)page1.getTotal(),pageSize,(int)page1.getPages(),page,pePermissionGroups);
    }


    //权限组之根据id删除数据
    @Override
    public R delete(Long id) {
        LambdaQueryWrapper<PermissionPermissionGroup> warpper = new LambdaQueryWrapper<>();
        warpper.eq(PermissionPermissionGroup::getPgid, id);
        permissionPermissionGroupMapper.delete(warpper);
        permissionGroupMapper.deleteById(id);
        return R.success("删除成功");
    }

    //权限组修改
    @Override
    public R updateById(Long id, Permissionss permissionss) {
        //更新名字
        PermissionGroup groupPermission = new PermissionGroup();
        groupPermission.setId(id);
        groupPermission.setName(permissionss.getTitle());
        groupPermission.setUpdateTime(LocalDateTime.now());
        permissionGroupMapper.updateById(groupPermission);
        //更新权限组 先删后增原则
        LambdaQueryWrapper<PermissionPermissionGroup> warpper = new LambdaQueryWrapper<>();
        warpper.eq(PermissionPermissionGroup::getPgid, id);
        permissionPermissionGroupMapper.delete(warpper);

        Long[] permissions = permissionss.getPermissions();
        for (Long permission : permissions) {
            PermissionPermissionGroup pg = new PermissionPermissionGroup();
            pg.setPgid(id);
            pg.setPid(permission);
            permissionPermissionGroupMapper.insert(pg);
        }
        return R.success("修改成功");
    }

    @Override
    public R add(Permissionss permissionss) {
        PermissionGroup groupPermission = new PermissionGroup();
        groupPermission.setCreateTime(LocalDateTime.now());
        groupPermission.setUpdateTime(LocalDateTime.now());
        groupPermission.setName(permissionss.getTitle());
        permissionGroupMapper.insert(groupPermission);
        Long[] permissions1 = permissionss.getPermissions();
        for (Long integer : permissions1) {
            PermissionPermissionGroup pag = new PermissionPermissionGroup();
            pag.setPgid(permissionss.getId());
            pag.setPid(integer);
            permissionPermissionGroupMapper.insert(pag);
        }

        return new R();

    }

    @Override
    public Permissionss findAll(Long id) {
        Long[] longs = permissionPermissionGroupMapper.seleteLon(id);
        PermissionGroup permissionGroup = permissionGroupMapper.selectById(id);
        Permissionss permissionss = new Permissionss();
        permissionss.setId(id);
        permissionss.setPermissions(longs);
        permissionss.setTitle(permissionGroup.getName());
        permissionss.setCreate_date(permissionGroup.getCreateTime());
        return permissionss;
    }

    @Override
    public R simp(Permissionss permissionss) {

        LambdaQueryWrapper<PermissionGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(PermissionGroup::getId, permissionss.getId());
        List<PermissionGroup> permissionGroups = permissionGroupMapper.selectList(wrapper);
        return null;
    }

}
